package cn.com.zhangwoo.microstar.homepage.activity;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.microstar.activity.R;
import cn.com.zhangwoo.microstar.adapter.ListCityAdapter;
import cn.com.zhangwoo.microstar.entity.CityUser;
import cn.com.zhangwoo.microstar.fragment.HomeFragment;
import cn.com.zhangwoo.microstar.http.ApplicationController;
import cn.com.zhangwoo.microstar.http.NetUtils;
import cn.com.zhangwoo.microstar.http.NormalPostRequest;
import cn.com.zhangwoo.microstar.inteface.IUtilDB;
import cn.com.zhangwoo.microstar.utildb.AppManager;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;

/**
 * 城市的选择 省
 * @author 张切
 */
public class CityActivity extends Activity implements OnItemClickListener,OnClickListener{
	private TextView mTextView;
	ListView mListView;
	Context context;
	HomeFragment home;
	private String indexAddress;
	public SharedPreferences sharedPreferences=ApplicationController.preferences;
	private Intent intent;
	private LinearLayout mLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		AppManager.getInstance().addActivity(CityActivity.this);
		setContentView(R.layout.activity_city_list1);
		context=CityActivity.this;
		mLayout=(LinearLayout) findViewById(R.id.loading_progress);
		initView();
		findViewById(R.id.signin_actionbar_title_img).setOnClickListener(this);
        mTextView=(TextView) findViewById(R.id.signin_actionbar_title_text);
        mTextView.setText("选择省份");
        home=new HomeFragment();
		showPostRequest();
	}
	
	private void initView() {
		mListView=(ListView)findViewById(R.id.city_list1_listView);
		mListView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		showItemIntent(position);
		
	}
	
	private void showItemIntent(int position) {
		if (indexAddress!=null) {
			/*intent=new Intent(context,AddressCityActivity.class);
			intent.putExtra("cityNames",listCityUser.get(position).getName().toString());
			intent.putExtra("position", position);
			startActivityForResult(intent, 1);*/
		}
		else
		{
			intent=new Intent(context,CityUserActivity.class);
			intent.putExtra("position", position);
			startActivity(intent);
		}
		
		
	}
	
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode==1) {
			if (resultCode==1) {
				if (data!=null) {
					/*String cityNameA=data.getStringExtra("AddressCityA");
					String cityNameB=data.getStringExtra("AddressCityB");
					CommonToast.showToast(getApplicationContext(), cityNameA+cityNameB, 1);
					intent=new Intent(context,AddReceiptAddressActivity.class);
					intent.putExtra("AddReceiptA",cityNameA);
					intent.putExtra("AddReceiptB", cityNameB);
					setResult(2, intent);
					CityActivity.this.finish();*/
				}
				
			}
		}
		
	}
	String path;
	public void showPostRequest() {
		if (!NetUtils.isNetworkAvailable(CityActivity.this)) {
			Toast.makeText(CityActivity.this, "网络未连接", 2).show();
			return;
		}
		 Map<String, String> map = new HashMap<String, String>();  
		 map.put("c", "district");
		 map.put("a", "getlist");
		  RequestQueue requestQueue =ApplicationController.getHttpQueue();
		  Request<JSONObject> request = new NormalPostRequest(IUtilDB.MESSAGE_URL,
		    new Response.Listener<JSONObject>() {
		        @Override
		        public void onResponse(JSONObject response) {
		        Gson gson=new Gson();
					CityUser ll=gson.fromJson(response.toString(), CityUser.class);
					if(ll.getData().size()>0)
					{
						showViewGone();
						mListView.setAdapter(new ListCityAdapter(ll.getData(),context));
					}
		        }
		    	}, new Response.ErrorListener() {
		        @Override
		        public void onErrorResponse(VolleyError error) {
		        	Toast.makeText(context, "网络异常", 2)
					.show();
		        }
		    	}, map);
		requestQueue.add(request);
	}

	@Override
	public void onClick(View v) {
		
		CityActivity.this.finish();
	}
	
	
	
	private void showViewGone()
	{
		mLayout.setVisibility(View.GONE);
		mListView.setVisibility(View.VISIBLE);
	}


}
